package com.lxw.station.task;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
//@Component
public class HelloTask {

    //    @Scheduled(cron ="0 */1 * * * ?")
    public void task1() {
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.info("task1 " + date);
    }

    //每3秒执行一次
//    @Scheduled(fixedDelay = 3000)
    public void task2() throws InterruptedException {
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.info("task2 " + date);
        Thread.sleep(6000);
    }

    //容器启动后,延迟10秒后再执行一次定时器,以后每10秒再执行一次该定时器。
//     @Scheduled(initialDelay = 10000, fixedRate = 10000)
    public void task3() {
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.info("task3 " + date);
    }

    @SneakyThrows
    @Scheduled(fixedRate = 3000)
    public void task4() {
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        log.info("task4 " + date);
        Thread.sleep(6000);
    }

    @SneakyThrows
    public static void main(String[] args) {
        URL httpUrl = new URL("https://download.csdn.net/source/get_pay_log_banner");
        HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
        conn.setRequestMethod("GET");
        InputStream inStream = conn.getInputStream();
        byte[] bytes = readInputStream(inStream);
        String s = new String(bytes);
        System.out.println("s = " + s);
    }
    private static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        return outStream.toByteArray();
    }
}
